package cst.neu.yuejian_31.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.astuetz.PagerSlidingTabStrip;

import cst.neu.yuejian_31.R;
import cst.neu.yuejian_31.fragment.MasterCommentFragment;
import cst.neu.yuejian_31.fragment.MasterIntroFragment;
import cst.neu.yuejian_31.fragment.MasterVideoFragment;

public class MasterDetailActivity extends BaseActivity {

    private PagerSlidingTabStrip pagersliding;
    private ViewPager viewpager;
    private TextView masterName;
    private String clubId,userId;
    private MasterCommentFragment masterCommentFragment=null;
    private MasterIntroFragment masterIntroFragment=null;
    private MasterVideoFragment masterVideoFragment=null;
    private DisplayMetrics dm;
    private int state;
    private int collectState;
    private String s;
    private ImageView favorImageButton,btn_return;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_masterdetail);
        btn_return= (ImageView) findViewById(R.id.imageButton_return_activitydetail);
        btn_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        init();
        initviews();

//        ClubCollectStateModel model=new ClubCollectStateModel();
//        model.getClubState(userId,clubId,this);
//        favorImageButton.setOnClickListener(this);

    }

    private void initviews() {
        Intent intent=getIntent();
//        Toast.makeText(this,intent.getStringExtra("club_name"), Toast.LENGTH_SHORT).show();
        clubId=intent.getStringExtra("club_id");
        userId=getUserId();
//        Toast.makeText(this, clubId+userId, Toast.LENGTH_SHORT).show();
        masterName= (TextView) findViewById(R.id.textView_mastername);
//        favorImageButton= (ImageView) findViewById(R.id.imageView_club_collect);
        masterName.setText(intent.getStringExtra("master_name"));
    }

    private void init() {
        dm = getResources().getDisplayMetrics();//获取屏幕密度
        viewpager=(ViewPager)findViewById(R.id.viewPager_master_detail);
        viewpager.setAdapter(new PagerAdapter(getSupportFragmentManager()));
        pagersliding=(PagerSlidingTabStrip) findViewById(R.id.pagerslidingtabstrip_master_detail);
        pagersliding.setViewPager(viewpager);
        setpagerstyle();//设置PagerSlidingTabStrip显示效果

    }
    private void setpagerstyle() {
        pagersliding.setShouldExpand(true); // 设置Tab是自动填充满屏幕的
        pagersliding.setDividerColor(Color.TRANSPARENT); // 设置Tab的分割线是透明的
        // 设置Tab底部线的高度
        pagersliding.setUnderlineHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 1, dm));
        // 设置Tab Indicator的高度
        pagersliding.setIndicatorHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 3, dm));
        // 设置Tab标题文字的大小
        pagersliding.setTextSize((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP,16, dm));
        pagersliding.setIndicatorColor(Color.parseColor("#7FB12E"));// 设置Tab Indicator的颜色
        // 设置选中Tab文字的颜色 (这是自定义的一个方法)
//        pagersliding.setSelectedTextColor(Color.parseColor("#7FB12E"));
        pagersliding.setTabBackground(0); // 取消点击Tab时的背景色
    }

//    @Override
//    public void onSuccess(ClubStateBean clubStateBean) {
//        state=clubStateBean.getState();
//        s=String.valueOf(state);
//        Toast.makeText(ClubDetailActivity.this,s, Toast.LENGTH_SHORT).show();
//        if(state==1){
//            favorImageButton.setImageResource(R.drawable.ic_collect);
//
//        }else {
//            favorImageButton.setImageResource(R.drawable.ic_uncollect);
//        }
//    }
//
//    @Override
//    public void onSuccess(ClubCollectBean clubCollectBean) {
//        collectState=clubCollectBean.getSuccess();
//        if(collectState==1){
//            favorImageButton.setImageResource(R.drawable.ic_collect);
//            Toast.makeText(ClubDetailActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
//
//        }else {
//            favorImageButton.setImageResource(R.drawable.ic_uncollect);
//            Toast.makeText(ClubDetailActivity.this, "取消收藏", Toast.LENGTH_SHORT).show();
//        }
//    }
//
//    @Override
//    public void onFail(String msg) {
//
//    }
//
//    @Override
//    public void onClick(View v) {
//        switch (v.getId()){
//            case R.id.imageView_club_collect:
//                doFavor();
//                break;
//        }
//    }
//
//    private void doFavor() {
//        ClubCollectModel favorModel=new ClubCollectModel();
//        favorModel.clubCollect(userId,clubId,this);
//    }

    //自定义ViewPagerAdapter子类
    private class PagerAdapter extends FragmentPagerAdapter {
        private String[] titles={"大师简介","大师视频","大师评论"};//显示在二级导航上的标题文字
        public PagerAdapter(FragmentManager fm)    {  super(fm);    }
        @Override
        public CharSequence getPageTitle(int position) {return titles[position]; }
        public int getCount()
        {
            return titles.length;
        }

        @Override
        public Fragment getItem(int position) { //根据位置返回具体某个导航上对应的Fragment
            switch (position)
            {   case 0: if(masterIntroFragment==null) {  masterIntroFragment=new MasterIntroFragment();  }
                return masterIntroFragment;
                case 1:
                    if(masterVideoFragment==null)  {  masterVideoFragment=new MasterVideoFragment();}
                    return masterVideoFragment;
                case 2: if(masterCommentFragment==null) {  masterCommentFragment=new MasterCommentFragment();  }
                    return masterCommentFragment;
                default: return null;
            }
        }
    }
}
